package MyDisplayObjects
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/** An object of this class can be clicked, and sends a COMPLETE event.  Depending on input parameters may be clicked multiple times or may be clicked only once,
	 * and then faded out.
	 */ 
	public class ClickableMC extends MovieClip
	{
		// public static constants
		public static var BACKGROUND_COLOR:uint;
		public static const OVER_COLOR:uint = 0xFFFFEE, LINE_COLOR:uint = 0x998888;
		public static const FILTER_ALPHA:Number = .8;
		// completely private
		private var filter:Shape = null;
		
		// objects which will be accessible with getter functions
		private var _autoDisable:Boolean = true; // after clicking does this button deselect
		private var _mouseOver:Boolean = false;
		private var _disabled:Boolean = false;
		// getters - restrict access to the private variables so they are read only
		public function get mouseOver():Boolean{return _mouseOver;}
		public function get autoDisable():Boolean{return _autoDisable;}
		public function get disabled():Boolean{return _disabled;}
		
		// private objects or variables that are for internal use only
		
		public function ClickableMC(color:uint, autoDisable:Boolean)
		{
			BACKGROUND_COLOR = color;
			this._autoDisable = autoDisable;
			// this event fires when the object has been added to display, this is necessary because the "parent" doesn't become active until the object has been added.
			this.addEventListener( Event.ADDED, handleAdded);
		}
		public function handleAdded(event:Event):void
		{
			// now we have access to the parent, which is necessary to check whether this object is within the parent's space.
			
			// add a listener for a mouse over event, which is the first step of dragging.
			this.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
		}
		
		///////////////////////////////		MOUSE INTERACTION FUNCTIONS ///////////////////////////////////////////////////
		/** Fires when user is over this object */
		private function handleMouseOver (event:MouseEvent):void
		{
			// remove the event listener so it doesn't keep firing and calling this function repeatedly
			this.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
			_mouseOver = true; // set a variable so that we can know, from outside of this object, whether we are currently over this draggable mc.
			redraw();
			// add new event listeners, responding to a mouse down (start of drag)
			this.addEventListener(MouseEvent.CLICK, handleClick);
			// add a mouse out listener in case user is simply passing over this object incidentily
			this.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
		}
		
		/** Fires when user presses down on this object.  Should only be called when user is over this object */
		private function handleClick (event:MouseEvent):void
		{
			if (this.autoDisable)
			{
				this._disabled = true;
				redraw();
				this.removeEventListener(MouseEvent.CLICK, handleClick);
				this.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
			}
			this.dispatchEvent(new ClickableMCEvent(ClickableMCEvent.CLICKED, name));
		}
		/** Fires when mouse leaves the space of this object, this should not be active when we are dragging */
		private function handleMouseOut (event:MouseEvent):void
		{
			// remove out and down listeners, place over listener
			this.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
			this.removeEventListener(MouseEvent.CLICK, handleClick);
			_mouseOver = false;
			redraw();
			this.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);			
		}
		public function set disabled(b:Boolean):void
		{
			if (b)
			{
				if (this.hasEventListener(MouseEvent.MOUSE_OVER)) this.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
				if (this.hasEventListener(MouseEvent.MOUSE_OUT)) this.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
				if (this.hasEventListener(MouseEvent.CLICK)) this.removeEventListener(MouseEvent.CLICK, handleClick);
				_disabled = true;
				redraw();
			} else
			{
				this.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
				_disabled = false;
				redraw();
			}
		}
		/** Draw based on current properties */
		public function redraw():void
		{
			var color:uint;
			if (_disabled)
			{
				if (filter == null)
				{
					filter = new Shape();
					filter.graphics.beginFill(0xFFFFFF, FILTER_ALPHA);
					filter.graphics.drawRect(0, 0, this.width, this.height);
					filter.graphics.endFill();
					this.addChild(filter);
				}
				color = BACKGROUND_COLOR;
				
			} else if (_mouseOver)
			{
				if (filter != null && filter.parent == this)
				{
					this.removeChild(filter);
					filter = null;
				}
				color = OVER_COLOR
			} else 
			{
				if (filter != null && filter.parent == this)
				{
					this.removeChild(filter);
					filter = null;
				}
				color = BACKGROUND_COLOR;
			}
			this.graphics.lineStyle(this.width/100, LINE_COLOR);
			this.graphics.beginFill(color);
			this.graphics.drawRect(0, 0, this.width-this.width/100*2, this.height-this.width/100*2);
			this.graphics.endFill();
		}
	}
}